// pages/xinx_list/xinx_list.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    array2: [
      '请选择类型',
      '招聘求职',
      '本地服务',
      '房屋租售',
      '生意转让',
      '汽车交易',
      '二手物品',
      '摄影',
      '装修/定制',
      '培训报班',
    ], 
    index2: 0,
    array3: ['智能排序', '火热推荐'],
    index3: 0,
    collection: false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var type = options.type
    if(type){
      var city = wx.getStorageSync('city')
      var user = wx.getStorageSync("userInfo")
      if (user) {
        if (city) {
          that.setData({ city, user, index2:type})
          this.getList();
        }
      } else {
        wx.showToast({
          title: "请先登录",
          icon: 'none',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/user/user'
          })
        }, 2000)
      }
    }else{
      wx.showToast({
        title: "异常",
        icon: 'none',
        duration: 2000
      })
      setTimeout(function () {
        wx.navigateBack();
      }, 2000)
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //普通选择器：
  bindPickerChange2: function (e) {
    let _this = this;
    if (e.detail.value != 0){
      _this.setData({
        index2: e.detail.value
      })
      _this.getList();
    }

  },
  //普通选择器：
  bindPickerChange3: function (e) {
    let _this=this;
    _this.setData({
      index3: e.detail.value
    })
    _this.getList();
  },

  /*收藏*/
  collection: function () {
    let that = this.data;
    var collection = that.collection
    var col = collection ? false : true;
    this.setData({
      collection: col
    })
  },

  /*拨号*/
  calling: function (e) {
    console.log(e);
    wx.showActionSheet({
      itemList: ['400-900-2250'],
      success: function (res) {
        console.log(res) //当点击400-900-2250就相当于点击了
        wx.makePhoneCall({
          phoneNumber: '400-900-2250', //此号码并非真实电话号码，仅用于测试  
          success: function () {
            console.log("拨打电话成功！")
          },
          fail: function () {
            console.log("拨打电话失败！")
          }
        })
        if (!res.cancel) {
          console.log(res.tapIndex)//console出了下标
        }
      }
    });
  },
  //获取列表
  getList:function(){
    let that=this;
    var data = { type: that.data.index2, city: that.data.city, order: that.data.index3}
    app.wxRequest('POST', '/Samecity/All/getlist',data, (res) => {
      if(res.data.code == 0){
        that.setData({
          list:res.data.data
        })
      }
      wx.showToast({
        title: res.data.msg,
        icon: 'none',
        duration: 2000
      })
    })
  }

})